package com.voltmao.webpro.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * Created by zhouyang on 2016/12/27.
 */
public class PropertiesFileUtils {

    private static PropertiesFileUtils instance;
    private Map<String, Map> containerMap = new HashMap();

    private PropertiesFileUtils() {

    }

    /**
     * 返回配置文件的属性信息
     *
     * @param fileUrl
     * @return
     */
    public static Map getProperties(String fileUrl) {
        if (instance == null) {
            instance = new PropertiesFileUtils();
        }


        if (instance.containerMap.get(fileUrl) == null) {
            Properties pro = new Properties();
            Map resultMap = new HashMap();
            try {

                InputStream inStr = PropertiesFileUtils.class.getClassLoader().getResourceAsStream(fileUrl);
                pro.load(new InputStreamReader(inStr, "UTF-8"));

                Iterator it = pro.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry entry = (Map.Entry) it.next();
                    Object key = entry.getKey();
                    Object value = entry.getValue();
                    //System.out.println(key +":"+value);
                    resultMap.put(key, value);
                }


            } catch (IOException e) {
                e.printStackTrace();
            }

            instance.containerMap.put(fileUrl, resultMap);
        }

        return instance.containerMap.get(fileUrl);
    }

}
